package com.work.demo.controller.juc;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.sql.SqlExecutor;
import com.aliyun.odps.Instance;
import com.aliyun.odps.Odps;
import com.aliyun.odps.OdpsException;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;
import com.work.demo.service.ImsvaluesService;
import com.work.demo.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.*;

@RestController
@RequestMapping("/DataBaseController")
@Slf4j
public class DataBaseController {
    private static final String DRIVER_NAME = "com.aliyun.odps.jdbc.OdpsDriver";

    @Autowired
    private ImsvaluesService imsvaluesService;

    @PostMapping("/buildDataBase")
    public R<List<Entity>> buildDataBase() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/wumei_smart";

        String userName = "root";

        String pwd = "123456";

        String tableName = "data_vfl_node1x";

        String connect = "id varchar(100),x0 double,x1 double,x2 double,x3 double,x4 double,x5 double,x6 double,x7 double,x8 double,x9 double,  x10 double,  x11 double,  x12 double,  x13 double,  x14 double,  x15 double,  x16 double,  x17 double,  x18 double,  x19 double,";

        String createSql = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + connect + " KEY idx_id (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";

        String truncateSql = "truncate table " + tableName;

        String insertSql = "INSERT INTO data_vfl_node1x (id, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

        DataSource ds = new SimpleDataSource(url, userName, pwd);
        Connection connection = ds.getConnection();

        SqlExecutor.execute(connection, createSql);
        SqlExecutor.execute(connection, truncateSql);
        List<Object[]> objectList = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            Object[] objects = new Object[21];
            for (int j = 0; j < objects.length; j++) {
                if (j == 0){
                    String idValue = "165132d6f52f1a651df";
                    objects[j] = idValue;
                }else {
                    objects[j] = 3.4;
                }

            }
            objectList.add(objects);


        }
        int[] ints = SqlExecutor.executeBatch(connection, insertSql, objectList);
        ds.getConnection().close();


        System.out.println(Arrays.toString(ints));


        return R.ok();
    }

    private static final String accessId = "LTAI5tCUBHN2Kpi89ZiVU8rW";
    private static final String accessKey = "MOtKax0kFMSH57Nj3hTqv9iEdbDCTy";
    private static final String endPoint = "http://service.cn-shanghai.maxcompute.aliyun.com/api";
    private static final String project = "linya_project";
    private static final String sql = "select * from JDBC_TEST;";

    @PostMapping("/sqlTest")
    public void sqlTest() {
        Account account = new AliyunAccount(accessId, accessKey);
        Odps odps = new Odps(account);
        odps.setEndpoint(endPoint);
        odps.setDefaultProject(project);
        Instance i;
        try {
            i = SQLTask.run(odps, sql);
            i.waitForSuccess();
            List<Record> records = SQLTask.getResult(i);
            for (Record r : records) {
                System.out.println(r.get(0).toString());
            }
        } catch (OdpsException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/maxcomputeTest")
    public void maxcomputeTest() throws SQLException {

//        try {
//            Class.forName("com.aliyun.odps.jdbc.OdpsDriver");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//            System.exit(1);
//        }
//
//        Connection conn = DriverManager.getConnection(
//                "jdbc:odps:<http://dt.cn-shanghai.maxcompute.aliyun.com>?project=<linya_dev>",
//                "LTAI5tCUBHN2Kpi89ZiVU8rW", "MOtKax0kFMSH57Nj3hTqv9iEdbDCTy");
//        ResultSet rs;
//
//        Statement stmt = conn.createStatement();
//        String sql = "SELECT * FROM JDBC_TEST";
//        stmt.executeQuery(sql);
//
//        ResultSet rset = stmt.getResultSet();
//        while (rset.next()) {
//            System.out.println(String.valueOf(rset.getInt(1)) + "\t" + rset.getString(2));
//        }
    }


    @PostMapping("/file/upload")
    public Long fileUpload(@RequestPart("file") MultipartFile file) throws IOException {
        String path = "C:\\Users\\fulls\\Desktop\\经典data_X.csv";

        log.info("size:{}", FileUtil.getInputStream(FileUtil.file(path)).available());
        return file.getSize();
    }


    @GetMapping("/delete/test")
    public void deleteTest() {
        imsvaluesService.selectAll();
    }
}
